import { IAccount } from '@/pages/System/Account/typing';
import { IPaging } from '@/typings';
import { roleList, userInfo } from '../../configs';
import { accountList } from './account';
import '@/utils/jsExtend';
import { checkAccount, checkPower } from 'mock/tools';

const account = [
  /** 获取账号分页 */
  {
    url: '/api/account/getPaging',
    method: 'post',
    response: (options: { body: Record<string, any> }) => {
      // 判断账号与权限
      if(checkAccount()) return { code: 401, errMsg: '您的账号已被禁用' };
      if(!checkPower(10040)) return { code: 403, errMsg: '抱歉，您无权限执行该操作' };

      const { body } = options;
      const keyword = body.keyword || '';
      let index = (body.pageCurrent - 1) * 20;
      // 筛选账号
      const data = accountList.filter((p) => p.account.includes(keyword) || p.nickName.includes(keyword));
      const result: IPaging<any> = {
        pageCurrent: body.pageCurrent,
        pageSize: body.pageSize || 20,
        total: data.length,
        pageData: data.slice(index, index + 20)
      };
      return { code: 200, data: result }
    }
  },

  /** 新增账号 */
  {
    url: '/api/addAccount',
    method: 'post',
    response: (options: { body: Record<string, any> }) => {
      // 判断账号与权限
      if(checkAccount()) return { code: 401, errMsg: '您的账号已被禁用' };
      if(!checkPower(10041)) return { code: 403, errMsg: '抱歉，您无权限执行该操作' };

      const { body } = options;
      const account: IAccount = {
        id: accountList.length + 1,
        headImgUrl: body.headImgUrl,
        nickName: body.nickName,
        mobile: body.mobile,
        account: body.account,
        role: '',
        roleKey: body.roleKey,
        power: [],
        creator: userInfo.nickName,
        creatorAccount: userInfo.account,
        date: new Date().format('yyyy-MM-dd')
      };
      roleList.forEach((p) => {
        if (p.id == body.roleKey) {
          account.power = p.power;
          account.role = p.name;
        }
      });
      accountList.push(account);
      return { code: 200, data: account };
    }
  },

  /** 编辑账号 */
  {
    url: '/api/editAccount',
    method: 'post',
    response: (options: { body: Record<string, any> }) => {
      // 判断账号与权限
      if(checkAccount()) return { code: 401, errMsg: '您的账号已被禁用' };
      if(!checkPower(10042)) return { code: 403, errMsg: '抱歉，您无权限执行该操作' };

      const { body } = options;
      let result = null;
      accountList.map((p) => {
        if (p.id == body.id) {
          for (const key in body) {
            p[key] = body[key];
          }
          result = p;
        }
      });
      return { code: 200, data: result };
    }
  },

  /** 删除账号 */
  {
    url: '/api/deleteAccount',
    method: 'post',
    response: (options: { body: Record<string, any> }) => {
      // 判断账号与权限
      if(checkAccount()) return { code: 401, errMsg: '您的账号已被禁用' };
      if(!checkPower(10043)) return { code: 403, errMsg: '抱歉，您无权限执行该操作' };

      const { body } = options;
      accountList.forEach((p, index) => {
        if (p.id == body.id) accountList.splice(index, 1);
      });
      return { code: 200, data: true }
    }
  },

  /** 禁用/启用账号 */
  {
    url: '/api/switchAccount',
    method: 'post',
    response: (options: { body: Record<string, any> }) => {
      // 判断账号与权限
      if(checkAccount()) return { code: 401, errMsg: '您的账号已被禁用' };
      if(!checkPower(10042)) return { code: 403, errMsg: '抱歉，您无权限执行该操作' };

      const { body } = options;
      accountList.forEach(p => {
        if (p.id == body.id) p.disabled = body.disabled;
      });
      return { code: 200, data: true };
    }
  }
];

export default account;
